<?php

include_once $_SERVER['DOCUMENT_ROOT'].'HelmesWorkTest/backend/database/ConnectionManager.php';
include_once $_SERVER['DOCUMENT_ROOT'].'HelmesWorkTest/backend/model/data/PersonSectorInvolvement.php';
include_once $_SERVER['DOCUMENT_ROOT'].'HelmesWorkTest/backend/model/dao/PersonSectorInvolvementDAO.php';
include_once $_SERVER['DOCUMENT_ROOT'].'HelmesWorkTest/GeneralConstants.php';
/**
 * Description of PersonSectorInvolvementDAOMySQLImpl
 *
 * @author Reinowin7
 */
class PersonSectorInvolvementDAOMySQLImpl implements PersonSectorInvolvementDAO
{
    private $dbConnection;
    private $preparedStatement;
    private $rowsAffected;

    public function __construct($dbConnection)
    {
        $this->dbConnection = $dbConnection;
    }
    
    public function insertPersonSectorInvolvement($personSectorInvolvement) 
    {
        $personFKPrepared = $this->mysql_prep($personSectorInvolvement->getPersonFK());
        $sectorFKPrepared = $this->mysql_prep($personSectorInvolvement->getSectorFK());
        $agreeToTermsPrepared = $this->mysql_prep($personSectorInvolvement->getAgreesToTerms());
        if ($agreeToTermsPrepared == 0 or is_null($agreeToTermsPrepared)){$agreeToTermsPrepared = 0;}
        $resultSet = null;
        $sql = 'INSERT INTO Person_sectors_involvement ' .  
                '(person_fk, sector_fk, agrees_to_terms) ' .   
                'VALUES ' .
                '(' . $personFKPrepared . ', ' . $sectorFKPrepared . ', ' . $agreeToTermsPrepared . ')';
        try 
        {
            $resultSet = mysql_query($sql, $this->dbConnection);

            if (mysql_affected_rows() == 1)
            {
                $generatedID = mysql_insert_id();
                if ($generatedID and $generatedID != 0)
                {
                    $personSectorInvolvement->setPersonSectorInvolvementID($generatedID);
                    ConnectionManager::getInstance()->closeResultSet($resultSet);
                    return INSERTION_WAS_SUCCESSFUL;
                }
                else
                {
                    ConnectionManager::getInstance()->closeResultSet($resultSet);
                    return INSERTION_WAS_NOT_SUCCESSFUL;
                }
            }
            else
            {
                ConnectionManager::getInstance()->closeResultSet($resultSet);
                return INSERTION_WAS_NOT_SUCCESSFUL;
            }
        }
        catch (Exception $e) 
        {
            ConnectionManager::getInstance()->closeResultSet($resultSet);
            $e->printStackTrace();
            return INSERTION_WAS_NOT_SUCCESSFUL;
        }
    }
    
    public function deletePersonSectorInvolvement($personID)
    {
        $personIDPrepared = $this->mysql_prep($personID);
        $resultSet = null;
        $sql = 'DELETE FROM Person_sectors_involvement ' . 
                'WHERE person_fk = ' . $personIDPrepared;
        try 
        {
            $resultSet = mysql_query($sql, $this->dbConnection);
            
            if ($resultSet)
            {
                ConnectionManager::getInstance()->closeResultSet($resultSet);
                return DELETION_WAS_SUCCESSFUL;
            }
            else
            {
                ConnectionManager::getInstance()->closeResultSet($resultSet);
                return DELETION_WAS_NOT_SUCCESSFUL;
            }
        }
        catch (Exception $e) 
        {
            ConnectionManager::getInstance()->closeResultSet($resultSet);
            $e->printStackTrace();
            return DELETION_WAS_NOT_SUCCESSFUL;
        }
    }
    
     //-------------------HELPER METHODS-----------------------------------------
    function mysql_prep($value)
    {
        $magic_quotes_active = get_magic_quotes_gpc();//boolean - true if the quotes thing is turned on
        $new_enough_php = function_exists("mysql_real_escape_string");//boolean - true if the function exists (php 4.3 or higher)
        if ($new_enough_php)
        {
            if ($magic_quotes_active)
            {
                $value = stripslashes($value);//if its a new version of php but has the quotes thing running, then strip the slashes it puts in
            }
            $value = mysql_real_escape_string($value);//if its a new version use the function to deal with characters
        }
        else
        {
            if (!$magic_quotes_active)//If its an old version, and the magic quotes are off use the addslashes function
            {
               $value = addslashes($value);
            }
        }
        return $value;
    }
}

?>
